<template>
  <div>
    <div class="pageheader">
      <XHeader :left-options="{backText:''}" :title="xheader.title" class="XHeader"></XHeader>
    </div>
    <div class="pagetop">
      <div class="flex flex-center topbar">
        <div class="flex-full inputbox flex flex-center block" @click="onSearchTap">
          <img class="icon" src="@/assets/img/ic_search.png"/>
          <input style="text-align: left" class="input flex-full" disabled placeholder="搜索商品"/>
          <!-- <div @click="onScanTap">
              <img class="icon" src="@/assets/img/ic_scanning.png">
          </div>-->
        </div>
      </div>

      <!--筛选排序-->
      <screen-item :isClass="false" @onSoso="onSoso" v-model="searchParams" :cateGoodsCount="cateGoodsCount" :supplierList="supplierList"/>

<!--      <div class="toptab" v-show="!isTabSanxia">-->
<!--        <div-->
<!--          v-for="(item,index) in tab"-->
<!--          :key="index"-->
<!--          class="item"-->
<!--          :class="tabIndex==index?'active':''"-->
<!--          @click="onTabTap(index)"-->
<!--        >{{item.text}}-->
<!--        </div>-->
<!--      </div>-->
    </div>
<!--    <div class="page" :style="isTabSanxia ? 'padding-top:85px' : 'padding-top: 130px'">-->
    <div class="page" style="padding-top:85px">
      <div class="classifybox">
        <div class="classify">
          <scroller
            lock-x
            :bounce="false"
            ref="scrollerClassify"
            :pullup-config="{content:'',upContent:'',downContent:'释放加载更多',loadingContent:'加载中...',}"
            height="-155"
            class="forbidScrollA forbidScroll"
          >
<!--            :height="isTabSanxia ? '-110' : '-155'"-->
            <div>
              <!--              <div-->
              <!--                v-for="(item,index) in classify"-->
              <!--                :key="index"-->
              <!--                class="classify-item"-->
              <!--                :class="!item.subCate.length?'':item.isFoldopen?'foldbox foldopen':'foldbox'"-->
              <!--              >-->
              <!--                <div-->
              <!--                v-for="(item,index) in classify"-->
              <!--                :key="index"-->
              <!--                class="classify-item"-->
              <!--                :class="isParentActive === index && item.subCate.length ? '' : item.isFoldopen?'foldbox foldopen':'foldbox'"-->
              <!--              >-->
              <div
                v-for="(item,index) in classify"
                :key="index"
                class="classify-item"
                :class="isParentActive === index ? ((item.sub_cates && item.sub_cates.length) ? 'foldbox foldopen' : '') : ((item.sub_cates && item.sub_cates.length) ? 'foldbox' : '')"
              >
                <div class="text" :class="isParentActive === index ? 'checked-back' : ''"
                     @click="onClassifyTap(item,index)">{{item.cate_name}}
                </div>
                <!--                <div class="fold" v-if="item.sub_cates.length">-->
                <div class="fold classify-child" v-if="isParentActive === index">
                  <div
                    class="text"
                    :class="isChildActive === subIndex ? 'checked-back' : ''"
                    v-for="(sub_cates,subIndex) in item.sub_cates"
                    :key="subIndex"
                    @click="onSubItemTap(sub_cates,subIndex,index)"
                  >{{sub_cates.cate_name}}
                  </div>
                </div>
              </div>
            </div>
          </scroller>
        </div>
        <div class="product">
          <scroller
            @on-pullup-loading="getProduct(searchParams)"
            lock-x
            ref="scroller"
            :pullup-config="{content:'',upContent:'',downContent:'释放加载更多',loadingContent:'加载中...',autoRefresh: false,clsPrefix: 'xs-plugin-pullup-'}"
            height="-155"
            :use-pullup="true"
            v-model="status"
            class="forbidScrollB forbidScroll"
          >
<!--            :height="isTabSanxia ? '-110' : '-155'"-->
            <div v-if="products">
              <div>
                <div
                  class="block"
                  style="padding-right:0"
                  v-for="(item,index) in products"
                  :key="index"
                >
                  <div class="product-item" :class="[isHasGoodsText(item) ? 'noStock' : '']" @click="onInfoTap(item)">
                    <div class="noStock-back" v-if="isHasGoodsText(item)"></div>
                    <div class="noStock-con">
                      <div class="supplier">
                        <chimage class="image" :src="item.goods_image_main" mode="aspectFill"></chimage>
                        <div class="supplier-text">{{textSupplier(item.supplier_id)}}</div>
                      </div>
                      <div class="noStock-tip" v-if="isHasGoodsText(item)">
                        <div class="noStock-tip-text">无货</div>
                      </div>
                    </div>
                    <div class="content">
                      <div style="flex:1">
                        <div class="title">{{item.goods_name}}</div>
                        <!--                        <div class="price">{{item.internal_price}}</div>-->
                      </div>

                      <div class="list-price">
                        <div class="price newPrice">{{textNewPrice}}￥{{item.internal_price}}</div>
                      </div>

                      <div class="list-price">
                        <div class="price oldPrice">{{textOldPrice(item.supplier_id)}}￥{{item.reference_price}}</div>
                      </div>

                      <div class="list-price" style="margin: 7px 0 5px">
                        <div class="discount" v-if="discountClass(item.discount_rate)" :class="discountClass(item.discount_rate)">
                          <chimage class="icon" :src="require('@/assets/img/icon_discount.png')"></chimage>
                          <span>{{item.discount_rate}}%</span>
                        </div>
                        <div v-if="!isHasGoodsText(item)" class="shop-btn" @click.stop="onAddToCart(item.goods_sku, item.goods_moq)"
                             style="right: -11px;padding-bottom: 0;bottom: -7px">
                          <img :src="require('@/assets/img/icon_item_gwc.png')"/>
                        </div>
                      </div>

                    </div>
                  </div>
                </div>
              </div>
              <load-more
                v-if="!more&&!products.length"
                :show-loading="false"
                :tip="'暂无数据'"
                background-color="#fbf9fe"
              ></load-more>
            </div>
          </scroller>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import Mall from '@/dll/mall'
  // import forbidScroll from '../../../mixin/forbidScroll'
  import shop from '@/mixin/shop'
  import screen from '@/mixin/screen'
  import ScreenItem from '../../../components/screen'

  export default {
    name: 'mallClassify',
    // mixins: [forbidScroll],
    mixins: [shop, screen],
    components: {
      ScreenItem
    },
    data () {
      return {
        xheader: {
          title: '分类'
        },
        tab: [
          // { value: 1, text: '自营' },
          // { value: -1, text: '京东' }
          // { value: -2, text: '严选' }
        ],
        tabIndex: 0,
        classify: [],
        classifyIndex: 0,
        subClassifyIndex: -1,
        products: [],
        limit: 20,
        page: 1,
        more: true,
        userInfo: {},
        status: {
          pullupStatus: 'default'
        },
        isParentActive: 0, // 父级选中class
        isChildActive: -1, // 子级选中class

        // 筛选
        cateGoodsCount: [],
        searchParams: {},
        supplierList: []
      }
    },
    created () {
      this.setClassify()
      this.init()
    },
    mounted () {
      this.onHidden()
    },
    watch: {
      // defaultAddress (val, oldVal) {
      //   if (oldVal && oldVal !== val) {
      //     this.init()
      //   }
      // }
    },
    computed: {
      isTabSanxia () {
        return parseInt(this.userInfo.operator_id) === 10 || this.isSanXia
      }
    },
    methods: {
      // 分类隐藏
      setClassify () {
        this.userInfo = this.globalData.userInfo
        if (this.isTabSanxia) { // 如果是三峡 不展示自营商品
          this.tab = [{ value: -1, text: '京东', show: this.isTabSanxia }, {
            value: -4,
            text: '苏宁',
            show: this.isTabSanxia
          }]
        } else {
          this.tab = [{ value: 1, text: '自营' }, { value: -1, text: '京东' }, { value: -4, text: '苏宁' }]
        }
      },
      onSearchTap () {
        this.$router.push({
          name: 'mallSearch',
          query: { cateId: this.classify[this.classifyIndex] ? this.classify[this.classifyIndex].cate_id : null }
        })
      },
      onTabTap (index) {
        this.tabIndex = index
        this.init()
      },
      onClassifyTap (item, index) {
        if (this.isParentActive == index) {
          this.isParentActive = -1
          return
        }
        this.isParentActive = index
        this.isChildActive = -1
        if (item.sub_cates && item.sub_cates.length) {
          item.isFoldopen = !item.isFoldopen
          setTimeout(() => {
            this.$refs.scrollerClassify.reset()
          }, 500)
          this.classifyIndex = index
          this.subClassifyIndex = -1
          this.page = 1
          this.more = true
          this.products = []
          this.getProduct(this.searchParams)
        } else {
          this.classifyIndex = index
          this.more = true
          this.products = []
          this.page = 1
          this.status.pullupStatus = 'disabled'
          this.$nextTick(function () {
            this.$refs.scroller.enablePullup()
            this.$refs.scroller.reset({
              top: 0
            })
            this.status.pullupStatus = 'default'
          })
          this.getProduct(this.searchParams)
        }
      },
      onSubItemTap (item, index, pindex) {
        this.isChildActive = index
        this.classifyIndex = pindex
        this.subClassifyIndex = index
        this.more = true
        this.products = []
        this.page = 1
        this.status.pullupStatus = 'disabled'
        this.$nextTick(function () {
          this.$refs.scroller.enablePullup()
          this.$refs.scroller.reset({
            top: 0
          })
          this.status.pullupStatus = 'default'
        })
        this.getProduct(this.searchParams)
      },
      onInfoTap (item) {
        this.onAuto()
        this.$router.push({ name: 'mallInfo', params: { id: item.goods_sku } })
      },
      init () {
        this.classify = []
        this.isParentActive = 0
        this.classifyIndex = 0
        this.subClassifyIndex = -1
        this.isChildActive = -1
        this.products = []
        this.page = 1
        this.more = true
        this.getClassify()
      },
      async getClassify () {
        let param = {
          cate_source: this.tab[this.tabIndex].value
        }
        this.$vux.loading.show({
          text: '加载中'
        })
        let res = await Mall.getCateList(param)
        this.$vux.loading.hide()
        if (res.code == 0) {
          if (!res.data.length) return
          this.classify = res.data.map((item, index) => {
            item.isFoldopen = false
            if (this.classifyIndex == index) {
              item.isFoldopen = true
            }
            return item
          })
          this.getProduct(this.searchParams)
        } else {
          this.$vux.alert.show({
            title: '提示',
            content: res.msg,
            onHide: () => {
              this.$router.back()
            }
          })
        }
      },

      onSoso (searchParams = {}) {
        // e.preventDefault()
        this.page = 1
        this.more = true
        this.products = []
        this.status.pullupStatus = 'disabled'
        this.$nextTick(function () {
          this.$refs.scroller.enablePullup()
          this.$refs.scroller.reset({
            top: 0
          })
          this.status.pullupStatus = 'default'
        })
        this.getProduct(searchParams)
      },

      async getProduct (searchParams = {}) {
        if (!this.more) {
          return
        }
        let param = {
          page: this.page,
          page_size: this.limit,
          area_code: this.defaultAddress.code
        }

        // if (!this.isSanXia) {
        //   param.source = this.tab[this.tabIndex].value
        // }
        let item = this.classify[this.classifyIndex]
        param.cate_id = item.cate_id

        if (item.sub_cates && this.subClassifyIndex >= 0 && item.sub_cates.length) {
          let sub_cates = item.sub_cates[this.subClassifyIndex]
          param.cate_id = sub_cates.cate_id
        }
        this.$vux.loading.show({
          text: '加载中'
        })
        param = Object.assign({}, searchParams, param)
        let res = await Mall.searchGoods(param)
        this.$vux.loading.hide()
        if (res.code == 0) {
          this.cateGoodsCount = res.data.cate_goods_count
          this.supplierList = res.data.supplier_list
          if (param.page >= res.data.page_total) {
            this.more = false
            this.$refs.scroller.disablePullup()
          } else {
            this.$refs.scroller.donePullup()
          }

          let products = [].concat(this.products, res.data.list)
          this.products = products.filter((item) => { // 过滤掉无货商品
            return parseFloat(item.stock_state) !== -1
          })

          // this.products = [].concat(this.products, res.data.list)
          if (!this.products.length) this.more = false
          this.page = this.page + 1
        } else {
          this.$refs.scroller.disablePullup()
          this.$vux.alert.show({
            title: '提示',
            content: res.msg
          })
        }
      }
    }
  }
</script>

<style lang="less" scoped>
  /deep/ html, /deep/ body {
    overflow: hidden;
  }

  /deep/ .pageheader {
    z-index: 2 !important;
  }

  .page {
    padding-top: 90px;
  }

  .pagetop {
    display: block;
    width: 100%;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 2;
  }

  .topbar {
    box-sizing: border-box;
    background-color: #fff;
    padding-top: 5px;
    padding-bottom: 5px;
  }

  .topbar .icon {
    width: 20px;
    height: 20px;
    padding: 0 5px;
    display: block;
  }

  .topbar .inputbox {
    background: #fbf8f7;
    height: 35px;
  }

  .searchSubmit {
    height: 35px;
    line-height: 35px;
    font-size: 12px;
    border: none;
    background-color: transparent;
    color: #351009;
    width: 60px;
  }

  .topbar .input {
    font-size: 12px;
    color: #351009;
    text-align: center;
    line-height: 17px;
  }

  .toptab {
    box-sizing: border-box;
    width: 100%;
    display: flex;
    padding-left: 12px;
    padding-right: 12px;
    height: 45px;
    background-color: #fff;
  }

  .toptab > .item {
    flex: 1;
    height: 100%;
    display: block;
    line-height: 45px;
    font-size: 14px;
    color: #351009;
    opacity: 0.2;
    text-align: center;
    position: relative;
  }

  .toptab > .item.active {
    opacity: 1;
  }

  .toptab > .item.active::after {
    position: absolute;
    content: "";
    width: 100%;
    bottom: 0;
    left: 0;
    height: 1px;
    background: #b0290f;
  }

  .classifybox {
    display: flex;
    padding: 12px;

    .classify {
      width: 80px;
      background-color: #fff;
    }

    .product {
      flex: 1;
    }
  }

  .classify-item {
    .text {
      height: 35px;
      line-height: 35px;
      font-size: 12px;
      padding: 0 10px;
      box-sizing: border-box;
      position: relative;
      color: #ae9f9b;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      width: 80px;
    }
  }

  .foldbox {
    > .text::after {
      content: "";
      display: block;
      height: 0px;
      width: 0px;
      border-left: 5px solid transparent;
      border-right: 5px solid transparent;
      border-top: 5px solid #ae9f9b;
      position: absolute;
      right: 5px;
      top: 50%;
      margin-top: -2.5px;
      transition: all 0.5s;
    }

    .fold {
      display: block;
      overflow: hidden;
      height: auto;
      max-height: 0;
      transition: all 0.5s;
    }

    .classify-child {
      background-color: #f2f1f0;
    }
  }

  .foldbox.foldopen {
    > .text::after {
      transform: rotate(180deg);
      /*border-top-color: #fff;*/
      border-top: 5px solid #ae9f9b;
    }

    /*> .text {*/
    /*  background-color: #b0290f;*/
    /*  color: #fbf8f7;*/
    /*}*/

    .fold {
      max-height: 10000px;
    }
  }

  .checked-back {
    background-color: #b0290f;
    color: #fbf8f7 !important;
  }

  .product-item {
    display: flex;
    flex-direction: row;
    align-items: center;
    padding: 12px 16px;
    background-color: #fff;
  }

  .product-item .check {
    display: block;
    height: 16px;
    width: 16px;
  }

  .product-item .image {
    width: 100px;
    height: 100px;
    display: block;
    background-size: contain;
    background-color: #ffffff;
  }

  .product-item .content {
    flex: 1;
    margin-left: 16px;
    display: flex;
    height: 80px;
    flex-direction: column;
  }

  .product-item .content .title {
    font-size: 12px;
    /*color: #9f8d89;*/
    color: #575757;
    line-height: 17px;
    height: 34px;
    font-weight: 500;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    text-overflow: ellipsis;
    overflow: hidden;
  }

  .product-item .content .price {
    font-size: 14px;
    color: #ae2309;
    font-weight: 600;
    line-height: 16px;
  }

  .product-item .content .quantity {
    display: flex;
    flex-direction: row;
    justify-content: flex-end;
    align-items: center;
  }

  .product-item .content .quantity .plus {
    display: block;
    width: 24px;
    height: 24px;
  }

  .product-item .content .quantity .minus,
  .product-item .content .quantity .plus {
    display: block;
    width: 24px;
    height: 24px;
    line-height: 22px;
    text-align: center;
    font-size: 16px;
    box-sizing: border-box;
    border: 1px solid #d4d4d4;
  }

  .product-item .content .quantity .num {
    display: block;
    width: 40px;
    height: 24px;
    line-height: 24px;
    font-size: 18px;
    color: #ae2309;
    text-align: center;
  }

  .list-price .newPrice {
    width: 100% !important;
  }
</style>
